<%--
    Document   : index
    Created on : Oct 14, 2012, 10:12:11 PM
    Author     : aspr0
--%>

<%@page import="is.web.htgen.table.Pager"%>
<%@page import="java.util.Arrays"%>
<%@page import="java.util.Collections"%>
<%@page import="is.xdumper.XdumperNoPathException"%>
<%@page import="is.web.xdumper.client.XdumperNoDumpItemsException"%>
<%@page import="is.xdumper.type.Entity"%>
<%@page import="is.web.xdumper.client.XdumperClient"%>
<%@page import="javax.ejb.RemoveException"%>
<%@page import="is.bean.entity.employee.EmployeeBeanRemoteHome"%>
<%@page import="is.bean.entity.department.Department"%>
<%@page import="is.bean.entity.department.DepartmentBeanRemote"%>
<%@page import="is.bean.entity.department.DepartmentBeanRemoteHome"%>
<%@page import="is.bean.entity.region.RegionBeanRemoteHome"%>
<%@page import="nullutil.NullUtil"%>
<%@page import="is.web.htgen.Message"%>
<%@page import="is.web.htgen.table.DepartmentTable"%>
<%@page import="is.web.htgen.form.DepartmentForm"%>
<%@page import="java.util.Collection"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.ejb.CreateException"%>
<%@page import="javax.ejb.FinderException"%>

<%
    //request.setCharacterEncoding("UTF-8");

    String rudHtml = "generation error";
    String createHtml = "generation error";
    String rudTitle = "View";
    String msgHtml = "";

    try
    {
        Context initial = new InitialContext();
        Object depRef = initial.lookup("ejb/DepartmentBean");
        Object empRef = initial.lookup("ejb/EmployeeBean");
        Object regRef = initial.lookup("ejb/RegionBean");

        DepartmentBeanRemoteHome departmentHome =
                (DepartmentBeanRemoteHome) PortableRemoteObject.narrow(depRef, DepartmentBeanRemoteHome.class);
        EmployeeBeanRemoteHome employeeHome =
                (EmployeeBeanRemoteHome) PortableRemoteObject.narrow(empRef, EmployeeBeanRemoteHome.class);
        RegionBeanRemoteHome regionHome =
                (RegionBeanRemoteHome) PortableRemoteObject.narrow(regRef, RegionBeanRemoteHome.class);

        DepartmentForm form = new DepartmentForm();

        form.setDepartmentHome(departmentHome);
        form.setEmployeeHome(employeeHome);
        form.setRegionHome(regionHome);
        createHtml = form.genCreate();

        if (NullUtil.isZeroPositiveInteger(request.getParameter("i")))
        {
            msgHtml = Message.info(request.getParameter("i"));
        }
        else if (NullUtil.isZeroPositiveInteger(request.getParameter("w")))
        {
            msgHtml = Message.warning(request.getParameter("w"));
        }
        else if (NullUtil.isZeroPositiveInteger(request.getParameter("e")))
        {
            msgHtml = Message.error(request.getParameter("e"));
        }

        if (NullUtil.isPositiveInteger(request.getParameter("id"))
                && request.getParameter("act") != null
                && request.getParameter("create") == null
                && !"find".equals(request.getParameter("act")))
        {
            Integer id = Integer.parseInt(request.getParameter("id"));
            DepartmentBeanRemote department = departmentHome.findByPrimaryKey(id);
            form.setBean(department);

            if ("update".equals(request.getParameter("act")))
            {
                rudHtml = form.genCopyUpdate(true);
                rudTitle = "Update";
%>

<jsp:useBean id="updateDepartment" class="is.bean.entity.department.Department"/> <%--FULL PATH!!!--%>
<jsp:setProperty name="updateDepartment" property="*"/>

<%
                if (request.getMethod().equals("POST"))
                {
                    department.setData(updateDepartment);
                    response.sendRedirect("?i=0");
                }
            }
            else if ("copy".equals(request.getParameter("act")))
            {
                rudHtml = form.genCopyUpdate(false);
                rudTitle = "Copy";
%>

<jsp:useBean id="copyDepartment" class="is.bean.entity.department.Department"/> <%--FULL PATH!!!--%>
<jsp:setProperty name="copyDepartment" property="*"/>

<%
                if (request.getMethod().equals("POST"))
                {
                    departmentHome.createInstance(copyDepartment);
                    response.sendRedirect("?i=4");
                }
            }
            else if ("delete".equals(request.getParameter("act")))
            {
                rudHtml = form.genDelete();
                rudTitle = "Delete";

                if (request.getMethod().equals("POST"))
                {
                    departmentHome.remove(id);
                    response.sendRedirect("?i=1");
                }
            }
            else
            {
                rudHtml = form.genRetrieve();
            }
        }
        else if ("create".equals(request.getParameter("create")) && request.getMethod().equals("POST"))
        {
%>

<jsp:useBean id="createDepartment" class="is.bean.entity.department.Department"/> <%--FULL PATH!!!--%>
<jsp:setProperty name="createDepartment" property="*"/>

<%
            departmentHome.createInstance(createDepartment);
            response.sendRedirect("?i=2");
        }
        else if ("dump".equals(request.getParameter("submit")) && request.getMethod().equals("POST"))
        {
            XdumperClient xdumperClient = new XdumperClient();
            xdumperClient.setRequest(request);
            rudHtml = xdumperClient.doExport(Entity.DEPARTMENT);
            msgHtml = Message.info("6");
        }
        else
        {
            Department departmentExample = new Department();
            DepartmentTable table = new DepartmentTable();
            table.setDepartmentHome(departmentHome);
            table.setEmployeeHome(employeeHome);
            table.setRegionHome(regionHome);
            Boolean searchPerformed = false;

            if ("find".equals(request.getParameter("act")))
            {
                try
                {
%>

<jsp:useBean id="findDepartment" class="is.bean.entity.department.Department"/> <%--FULL PATH!!!--%>
<jsp:setProperty name="findDepartment" property="*"/>

<%
                    departmentExample = findDepartment;
                    searchPerformed = true;
                }
                /* Lazy catching nested number format exception */
                catch (Exception e)
                {
                    msgHtml = Message.warning("0");
                }
            }

            int start;
            try
            {
                start = Integer.parseInt(request.getParameter("s"));
            }
            catch (NumberFormatException e)
            {
                start = 1;
            }

            int end = start + 99;

            Collection<DepartmentBeanRemote> departments = departmentHome.findByExample(departmentExample, start, end);
            table.setDepartments(departments);
            table.setFindExample(departmentExample);
            table.setStart(start);
            table.setCount(departmentHome.countFind(departmentExample));

            if (searchPerformed)
            {
                if (departments.size() < 1)
                {
                    msgHtml = Message.warning("3");
                }
                else
                {
                    msgHtml = Message.info("3");
                }
            }

            if("go".equals(request.getParameter("pager")))
            {
                response.sendRedirect(Pager.generateQuery(request.getParameterMap(),
                                      Integer.parseInt(request.getParameter("start"))));
            }

            rudHtml = table.generate();
        }
    }
    catch (RemoveException e)
    {
        //e.printStackTrace();
        response.sendRedirect("?w=2");
    }
    catch (CreateException e)
    {
        //e.printStackTrace();
        response.sendRedirect("?w=1");
    }
    catch (FinderException e)
    {
        //e.printStackTrace();
        response.sendRedirect("?w=5");
    }
    catch (XdumperNoDumpItemsException e)
    {
        response.sendRedirect("?w=6");
    }
    catch (XdumperNoPathException e)
    {
        response.sendRedirect("?w=9");
    }
    catch (Exception e)
    {
        e.printStackTrace();
        response.sendRedirect("?e=1");
    }
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=is.web.htgen.Locale.TITLE%></title>
        <link rel="stylesheet" href="../style/common.css">
        <link rel="stylesheet" href="../style/rud-table.css">
        <link rel="stylesheet" href="../style/msg.css">
        <link rel="stylesheet" href="../style/xdumper.css">

        <script src="../script.js" type="text/javascript"></script>
    </head>
    <body>
        <h1><a href=".."><img src="../img/go-home_32.png" class="linkIcon"></a>| Departments</h1>

        <%=msgHtml%>

        <div class="spoiler" onclick="toggleDisplay('frameCreate');">Create new one</div>
        <div class="spoiled" id="frameCreate" style="display: none;">
            <%=createHtml%>
        </div>

        <div class="spoiler" onclick="toggleDisplay('rudHtml');"><%=rudTitle%></div>
        <div class="spoiled" id="rudHtml">
            <%=rudHtml%>
        </div>


    </body>
</html>
